<template>
  <div class="body">
    <div class="stars" ref="starsRef">
      <div class="star" v-for="(item, index) in starsCount" :key="index"></div>
    </div>
    <div id="building">
    <div class="handle-box">
      <el-select v-model="searchid" placeholder="请选择筛选方式">
        <el-option v-for="item in options1" :label="item.label" :key="item.key" :value="item.value">
        </el-option>
      </el-select>
      <el-input placeholder="筛选信息" v-model="searchWord" style="width: 100px"></el-input>
      <el-button type="primary" @click="searchfor(searchid)">查询信息</el-button>
      <el-button type="primary" @click="dialogVisible1= true">添加信息</el-button>
    </div>
    <el-table height="550px" border size="small" :data="nurseData.slice((currentpage - 1)*pageSize, currentpage * pageSize)" style="width: 100%" >
      <el-table-column label="工号" prop="nurseId" width="140" align="center"></el-table-column>
      <el-table-column label="姓名" prop="nurseName" width="140" align="center"></el-table-column>
      <el-table-column label="性别" prop="nurseSex" width="140" align="center"></el-table-column>
      <el-table-column label="年龄" prop="nurseAge" width="140" align="center"></el-table-column>
      <el-table-column label="电话" prop="nurseTel" width="140" align="center"></el-table-column>
      <el-table-column   label="操作" width="140" align="center">
        <template slot-scope="scope">
          <el-button @click.native.prevent="editRow(scope.row)" type="text" size="small">
            修改
          </el-button>
        </template>
      </el-table-column>
      <el-table-column   label="操作" width="140" align="center">
        <template slot-scope="scope">
          <el-button @click.native.prevent="deleteRow(scope.row.nurseId)" type="text" size="small">
           删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, prev, pager, next, jumper"
      :current-page="currentpage"
      :page-size="pageSize"
      :total="nurseData.length"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
</div>
    <!-- 添加弹窗-->
    <el-dialog title="增加信息"
               :visible.sync="dialogVisible1"
               width="30%" center>
      <el-form label-width="80px" :model="registerForm"  :rules="rules">
        <el-form-item label="工号" prop="nurseId">
          <el-input v-model="registerForm.nurseId"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="nurseName">
          <el-input v-model="registerForm.nurseName"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="nurseSex">
          <el-radio-group v-model="registerForm.nurseSex">
            <el-radio label="0">女</el-radio>
            <el-radio label="1">男</el-radio>
            <el-radio label="2">保密</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄" prop="nurseAge">
          <el-input  v-model="registerForm.nurseAge"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="nurseTel">
          <el-input type="textarea" v-model="registerForm.nurseTel"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible1=false">取 消</el-button>
        <el-button type="primary" @click="addnurse()">确 定</el-button>
      </span>
      </template>
    </el-dialog>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑信息" :visible.sync="dialogVisible2"
               width="30%" center>
      <el-form label-width="80px" :model="editForm"  :rules="rules">
        <el-form-item label="工号" prop="nurseId">
          <el-input v-model="editForm.nurseId" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="nurseName">
          <el-input v-model="editForm.nurseName"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="nurseSex">
          <el-radio-group v-model="editForm.nurseSex"  :disabled="true">
            <el-radio :label="0" >女</el-radio>
            <el-radio :label="1" >男</el-radio>
            <el-radio :label="2" >保密</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄" prop="nurseAge">
          <el-input  v-model="editForm.nurseAge"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="nurseTel">
          <el-input type="textarea" v-model="editForm.nurseTel"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible2=false">取 消</el-button>
        <el-button type="primary" @click="saveEdit()">确 定</el-button>
      </span>
      </template>
    </el-dialog>

    <!-- 移除提示框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible3"
               width="30%"  center>
      <div class="del-dialog-cnt" align="center">是否确定删除？</div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible3=false">取 消</el-button>
          <el-button type="primary" @click="deletenurse()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {onMounted, ref} from "vue";

export default {
  name: "message",
  data(){
    return{
      nurseData:[
        {
          nurseId:'2',
          nurseName:'熊诗伟',
          nurseSex:'女',
          nurseAge:'21',
          nurseTel:'6.1',
        },
        {
          nurseId:'2',
          nurseName:'熊诗伟',
          nurseSex:'女',
          nurseAge:'21',
          nurseTel:'6.1',
        },
        {
          nurseId:'2',
          nurseName:'熊诗伟',
          nurseSex:'女',
          nurseAge:'21',
          nurseTel:'6.1',
        },
      ],
      options1:[
        {
          value:'1',
          label:"工号",
          key:'1'
        },
        {
          label: "姓名",
          value:'2',
          key:'2'
        },],
      searchid:'',
      searchWord:'',
      pageSize:'10',
      sexnumber:'',
      currentpage:'1',
      dialogVisible1:false,
      dialogVisible2:false,
      dialogVisible3:false,
      editForm:{
        nurseId:'',
        nurseName:'',
        nurseSex:'',
        nurseAge:'',
        nurseTel:'',
      },
      registerForm:{
        nurseId:'',
        nurseName:'',
        nurseSex:'',
        nurseAge:'',
        nurseTel:'',
      },
      rules: {
        nurseId: [
          {required: true, message: " 工号不可为空", trigger: 'blur'}
        ],
        nurseName: [
          {required: true, message: " 姓名不能为空 ", trigger: 'blur'}
        ],
        nurseSex: [
          {required: true, message: " 性别不可为空 ", trigger: 'blur'}
        ],
        nurseAge: [
          {required: true, message: " 年龄不可为空 ", trigger: 'blur'}
        ],
        nurseTel: [
          {required: true, message: " 电话不可为空 ", trigger: 'blur'}
        ],
      },
      deleteId:'',
    }
  },
  created(){
    axios.get('http://localhost:8080/Nurse/queryNurseList',{params:{startIndex:0,pageSize:1000}}).then(res=>{

      this.nurseData=res.data.nurseData;
    })
  },
  methods:{
    handleSizeChange(val) { //每页条数
      this.pageSize = val;
    },
    handleCurrentChange(val) { //跳转页码
      this.currentpage= val;
    },
    searchfor(id){
      let xxd1='';
      let xxd2='';
      if(id==1){
        xxd1=this.searchWord;
      }else{
        xxd2=this.searchWord;
      }
      axios.get('http://localhost:8080/Nurse/queryNurseByKey',{params:{startIndex:0,pageSize:1000,nurseID:xxd1,nurseName:xxd2}}).then(res=>{

        this.nurseData=res.data.nurseData;
      })
    },
    addnurse(){
      if(this.registerForm.nurseSex = 0) {
        this.registerForm.nurseSex = '女';
      }
      else if(this.registerForm.nurseSex = 1){
        this.registerForm.nurseSex = '男';
      }
      else{
        this.registerForm.nurseSex = '保密';
      }
      axios.post('http://localhost:8080/Nurse/insertNurse',this.registerForm).then(res=>{
          if(res.data==1){
          this.$message({
            message: '新增成功'
          });
            this.$router.go(0);
        }
        else if(res.data==0){
          this.$message({
            message: '增加失败',
            type:'warning'
          });
        }
        else{
            this.$message({
              message: '工号重复',
              type:'warning'
            });
          }
      });
      this.dialogVisible1=false
    },
    saveEdit(){
      if(this.editForm.nurseSex = 0) {
        this.editForm.nurseSex = '女';
      }
      else if(this.editForm.nurseSex = 1){
        this.editForm.nurseSex = '男';
      }
      else{
        this.editForm.nurseSex = '保密';
      }
      axios.put('http://localhost:8080/Nurse/updateNurse',this.editForm).then(res=>{
        if(res.data==1){
          this.$message({
            message: '编辑成功'
          });
          this.$router.go(0);
        }
        else{
          this.$message({
            message: '编辑失败',
            type:'warning'
          });
        }
      });
      this.dialogVisible2=false
    },
    deletenurse(){
      let ID=this.deleteId;
      axios.delete('http://localhost:8080/Nurse/deleteNurse',{data:ID}).then(res=>{
        if(res.data==1){
          this.$message({
            message: '删除成功'
          });
          this.$router.go(0);
        }
        else{
          this.$message({
            message: '删除失败,没有该人',
            type:'warning'
          });
        }
      });
      this.dialogVisible3=false
    },
    editRow(row){
      this.dialogVisible2 = true;
      this.editForm.nurseId=row.nurseId;
      this.editForm.nurseName=row.nurseName;
      if(row.nurseSex=='女') {
        this.editForm.nurseSex = 0;
      }
      else if(row.nurseSex=='男'){
        this.editForm.nurseSex = 1;
      }
      else{
        this.editForm.nurseSex = 2;
      }
      this.editForm.nurseAge=row.nurseAge;
      this.editForm.nurseTel=row.nurseTel;
    },
    deleteRow(nurseId){
      this.dialogVisible3 = true;
      this.deleteId=nurseId;
    },
  },
  setup() {
    let starsRef = ref(null);

    const starsCount = 800; //星星数量
    const distance = 900; //间距

    onMounted(() => {
      let starNodes = Array.from(starsRef.value.children);
      starNodes.forEach((item) => {
        let speed = 0.2 + Math.random() * 1;
        let thisDistance = distance + Math.random() * 300;
        item.style.transformOrigin = `0 0 ${thisDistance}px`;
        item.style.transform = `
        translate3d(0,0,-${thisDistance}px)
        rotateY(${Math.random() * 360}deg)
        rotateX(${Math.random() * -50}deg)
        scale(${speed},${speed})`;
      });
    });

    return {
      starsRef,
      starsCount,
    };
  },
}
</script>

<style scoped>
#building {
  /*设置透明度，0为完全透明，1为不透明*/
  opacity: 0.50;
}
.body {
  position: absolute;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  background: radial-gradient(
    200% 100% at bottom center,
    #f7f7b6,
    #e96f92,
    #1b2947
  );
  background: radial-gradient(
    200% 105% at top center,
    #1b2947 10%,
    #75517d 40%,
    #e96f92 65%,
    #f7f7b6
  );
  background-attachment: fixed;
  overflow: hidden;
}

@keyframes rotate {
  0% {
    transform: perspective(400px) rotateZ(20deg) rotateX(-40deg) rotateY(0);
  }
  100% {
    transform: perspective(400px) rotateZ(20deg) rotateX(-40deg)
    rotateY(-360deg);
  }
}
.stars {
  transform: perspective(500px);
  transform-style: preserve-3d;
  position: absolute;
  perspective-origin: 50% 100%;
  left: 45%;
  animation: rotate 90s infinite linear;
  bottom: 0;
}
.star {
  width: 2px;
  height: 2px;
  background: #f7f7b6;
  position: absolute;
  left: 0;
  top: 0;
  backface-visibility: hidden;
}
</style>
